package com.qyhl.school.school;

import android.view.MenuItem;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.gyf.immersionbar.ImmersionBar;
import com.qyhl.school.R;
import com.qyhl.school.R2;
import com.qyhl.school.school.center.SchoolCenterFragment;
import com.qyhl.school.school.column.SchoolColumnFragment;
import com.qyhl.school.school.home.newhome.SchoolNewMainFragment;
import com.qyhl.school.school.reporter.SchoolReporterFragment;
import com.qyhl.school.school.vlog.SchoolVlogFragment;
import com.qyhl.webtv.basiclib.base.BaseActivity;
import com.qyhl.webtv.basiclib.base.BaseIViewPresenter;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

@Route(path = ARouterPathConstant.SCHOOL_HOME)
public class SchoolHomeActivity extends BaseActivity {
    @BindView(R2.id.bottom_view)
    BottomNavigationView bottomView;

    private int lastIndex;
    private List<Fragment> mFragments;
    private final int[] ids = new int[]{R.id.home, R.id.reporter, R.id.column, R.id.vlog, R.id.user_center};

    @Override
    protected int getContentView() {
        return R.layout.activity_school_home;
    }

    @Override
    protected void setImmersionBar(ImmersionBar mImmersionBar) {
        mImmersionBar.statusBarDarkFont(false).init();
    }

    @Override
    protected BaseIViewPresenter initBaseIViewPresenter() {
        return null;
    }

    @Override
    protected void init() {
        // 解决当item大于三个时，非平均布局问题
        BottomNavigationViewHelper.disableShiftMode(bottomView);

        mFragments = new ArrayList<>();
        mFragments.add(SchoolNewMainFragment.newInstance());
        mFragments.add(SchoolReporterFragment.newInstance());
        mFragments.add(SchoolColumnFragment.newInstance());
        mFragments.add(SchoolVlogFragment.newInstance(true));
        mFragments.add(SchoolCenterFragment.newInstance());
        // 初始化展示MessageFragment
        setFragmentPosition(0);
    }

    @Override
    protected void setListener() {
        bottomView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                int itemId = item.getItemId();
                if (itemId == R.id.home) {
                    setFragmentPosition(0);
                } else if (itemId == R.id.reporter) {
                    setFragmentPosition(1);
                } else if (itemId == R.id.column) {
                    setFragmentPosition(2);
                } else if (itemId == R.id.vlog) {
                    setFragmentPosition(3);
                } else if (itemId == R.id.user_center) {
                    setFragmentPosition(4);
                }
                // 这里注意返回true,否则点击失效
                return true;
            }
        });

        //取消长按Toast
        View menuView = bottomView.getChildAt(0);
        for (int id : ids) {
            menuView.findViewById(id).setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    return true;
                }
            });
        }
    }

    private void setFragmentPosition(int position) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        Fragment currentFragment = mFragments.get(position);
        Fragment lastFragment = mFragments.get(lastIndex);
        lastIndex = position;
        ft.hide(lastFragment);
        if (!currentFragment.isAdded()) {
            getSupportFragmentManager().beginTransaction().remove(currentFragment).commit();
            ft.add(R.id.frameLayout, currentFragment);
        }
        ft.show(currentFragment);
        ft.commitAllowingStateLoss();
    }

    public void doFinish() {
        finish();
    }

    public void showMoreSchool() {
        setFragmentPosition(2);
    }

}
